package com.think.teaching.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.think.teaching.mapper.ActionDataMapper;
import com.think.teaching.model.ActionData;
import com.think.teaching.model.ActionType;
import com.think.teaching.service.ActionDataService;

@Service
public class ActionDataServiceImpl extends BaseServiceImpl<ActionDataMapper, ActionData> implements ActionDataService{

	
	@Override
	public ActionData AddActionData(ActionData actionData) {

		if (this.save(actionData)) {
			return actionData;
		}
		return null;
	}


	@Override
	public ActionData UpdateActionData(ActionData actionData) {
		if (this.updateById(actionData)) {
			return actionData;
		}
		return null;
	}

	@Override
	public ActionData GetActionData(Integer id,Boolean withoutBan) {
		QueryWrapper<ActionData> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return getOne(queryWrapper);
	}

	@Override
	public ActionData SetActionDataState(Integer id, Integer state) {
		UpdateWrapper<ActionData> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}


	@Override
	public List<ActionData> ListActionDataSearch(String key,Integer type, Boolean withoutBan) {
		QueryWrapper<ActionData> queryWrapper = new QueryWrapper<>();
		
		queryWrapper.ne("delflag", 1).eq("type_id", type).like("action_value", key);	
		
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		queryWrapper.last("limit 0,10");
		return list(queryWrapper);
	}
	
	@Override
	public IPage<ActionData> ListActionDataWithType(Integer count, Integer page, Integer type,
			Boolean withoutBan) {
		QueryWrapper<ActionData> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).eq("type_id", type);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<ActionData> ipage = new Page<>(page, count);
		return this.page(ipage,queryWrapper);
	}
	
	@Override
	public ActionData GetActionDataByType(Integer typeId) {
		QueryWrapper<ActionData> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).eq("type_id", typeId);
		return this.getOne(queryWrapper);
	}
}
